package com.breakfast.fun.ui.user.coupon;

import com.breakfast.fun.App;
import com.breakfast.fun.Constant;
import com.breakfast.fun.data.entity.user.coupon.Coupon;
import com.breakfast.fun.util.Api;
import com.breakfast.fun.util.JumpUtil;
import com.breakfast.fun.util.L;
import com.breakfast.fun.util.T;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yukai on 2016/11/9.
 */

public class CouponPresenter extends CouponContract.Presenter {

    @Override
    public void onStart() {
        getCoupon(0, false);
    }

    @Override
    public void getCoupon(int begin, boolean isLoadMore) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        if (Constant.getUserData() != null) {
            map.put("userId", Constant.getUserData().getId() + "");
            map.put("begin", begin + "");
            map.put("end", "5");

        } else {
            JumpUtil.toLogin(App.getAppContext());
            return;
        }

        showLoadingDialog(true);

        rxManager.add(model.getCouponModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Coupon>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.show("获取数据失败");
                        hideLoadingDialog();
                        view.stopRefresh();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Coupon coupon) {
                        hideLoadingDialog();
                        view.stopRefresh();
                        if (coupon.isSuccess()) {
                            if (isLoadMore) {
                                view.loadCoupon(coupon);
                            } else {
                                view.showCoupon(coupon);
                            }
                        } else {
                            T.show(coupon.getMsg());
                        }
                    }

                }));
    }

    @Override
    public void getCouponTimeOut(int begin, boolean isLoadMore) {
        Map<String, String> map = Api.getInstance().getBaseParams();

        if (Constant.getUserData() != null) {
            map.put("userId", Constant.getUserData().getId() + "");
            map.put("begin", begin + "");
            map.put("end", "2");

        } else {
            JumpUtil.toLogin(App.getAppContext());
            return;
        }

        showLoadingDialog(true);

        rxManager.add(model.getCouponTimeOutModel(map)
                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Coupon>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        T.show("获取数据失败");
                        hideLoadingDialog();
                        view.stopRefresh();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(Coupon coupon) {
                        hideLoadingDialog();
                        view.stopRefresh();
                        if (coupon.isSuccess()) {
                            if (isLoadMore) {
                                view.loadCoupon(coupon);
                            } else {
                                view.showCoupon(coupon);
                            }
                        } else {
                            T.show(coupon.getMsg());
                        }
                    }

                }));
    }
}
